/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package org.dromara.setting.controller.app;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.ServletUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.generator.util.VelocityInitializer;
import org.dromara.setting.enums.SettingAllEnums.AgreementEnums;
import org.dromara.setting.service.ISettingConfigItemService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.StringWriter;

import static org.dromara.common.core.constant.Constants.UTF8;

/**
 * 用户协议模块-前端API
 *
 * @author heyi
 * 2024/8/13
 */

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/setting/agreement")
public class AgreementController extends BaseController {

    private final ISettingConfigItemService settingConfigItemService;

    /**
     * 隐私政策Html
     */
    @SaIgnore
    @RequestMapping(path = "/privacyPolicyHtml", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String privacyPolicyHtml() {
        //获取请求头里租户id
        String tenantId = ServletUtils.getRequest().getHeader(LoginHelper.TENANT_KEY);
        String content = TenantHelper.dynamic(tenantId, () -> {
            //根据租户id不同查询不同内容
            return settingConfigItemService.selectValueByKey(AgreementEnums.AGREEMENT, AgreementEnums.PRIVACY_POLICY.getKey()).getValue();
        });
        return getHtml("隐私政策", content);
    }

    /**
     * 用户协议Html
     */
    @SaIgnore
    @RequestMapping(path = "/userAgreementHtml", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String userAgreementHtml() {
        //获取请求头里租户id
        String tenantId = ServletUtils.getRequest().getHeader(LoginHelper.TENANT_KEY);
        String s = TenantHelper.dynamic(tenantId, () -> {
            //根据租户id不同查询不同内容
            return settingConfigItemService.selectValueByKey(AgreementEnums.AGREEMENT, AgreementEnums.USER_AGREEMENT.getKey()).getValue();
        });
        return getHtml("用户协议", s);
    }

    /**
     * 关于我们Html
     *
     * @return
     */
    @SaIgnore
    @RequestMapping(path = "/aboutAppHtml", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String aboutAppHtml() {
        //获取请求头里租户id
        String tenantId = ServletUtils.getRequest().getHeader(LoginHelper.TENANT_KEY);
        String s = TenantHelper.dynamic(tenantId, () -> {
            //根据租户id不同查询不同内容
            return settingConfigItemService.selectValueByKey(AgreementEnums.AGREEMENT, AgreementEnums.ABOUT_APP.getKey()).getValue();
        });
        return getHtml("关于我们", s);
    }

    /**
     * 隐私政策
     */
    @SaIgnore
    @GetMapping("/privacyPolicy")
    public R<String> privacy_policy() {
        //获取请求头里租户id
        String tenantId = ServletUtils.getRequest().getHeader(LoginHelper.TENANT_KEY);
        String content = TenantHelper.dynamic(tenantId, () -> {
            //根据租户id不同查询不同内容
            return settingConfigItemService.selectValueByKey(AgreementEnums.AGREEMENT, AgreementEnums.PRIVACY_POLICY.getKey()).getValue();
        });
        return R.ok("", content);
    }

    /**
     * 用户协议
     */
    @SaIgnore
    @GetMapping("/userAgreement")
    public R<String> userAgreement() {
        //获取请求头里租户id
        String tenantId = ServletUtils.getRequest().getHeader(LoginHelper.TENANT_KEY);
        String s = TenantHelper.dynamic(tenantId, () -> {
            //根据租户id不同查询不同内容
            return settingConfigItemService.selectValueByKey(AgreementEnums.AGREEMENT, AgreementEnums.USER_AGREEMENT.getKey()).getValue();
        });
        return R.ok("", s);
    }

    /**
     * 关于我们
     *
     * @return
     */
    @SaIgnore
    @GetMapping("/aboutApp")
    public R<String> aboutApp() {
        //获取请求头里租户id
        String tenantId = ServletUtils.getRequest().getHeader(LoginHelper.TENANT_KEY);
        String s = TenantHelper.dynamic(tenantId, () -> {
            //根据租户id不同查询不同内容
            return settingConfigItemService.selectValueByKey(AgreementEnums.AGREEMENT, AgreementEnums.ABOUT_APP.getKey()).getValue();
        });
        return R.ok("", s);
    }


    private String getHtml(String title, String content) {
        VelocityInitializer.initVelocity();
        VelocityContext context = new VelocityContext();
        context.put("title", title);
        context.put("content", content);
        // 渲染模板
        StringWriter sw = new StringWriter();
        Template tpl = Velocity.getTemplate("vm/index.html.vm", UTF8);
        tpl.merge(context, sw);
        String result = sw.toString();
        return result;
    }
}
